package com.andatsoft.laisim.audioeffect;

import android.content.Context;
import android.media.audiofx.BassBoost;
import android.media.audiofx.Equalizer;

import com.andatsoft.laisim.audioeffect.AudioEffect.ApplyMode;
import com.andatsoft.laisim.config.LaisimConfig;
import com.andatsoft.laisim.db.DataBaseAccess;

public class AudioEffectManager {

	public static AudioEffect mAudioEffect;
	public static Equalizer equalizer;
	public static BassBoost bassBoost;
	public static boolean isSoundEffectSupported = true;

	public static boolean initAudioEffect(int audioSessionId) {
		mAudioEffect = new AudioEffect();
		try {
			equalizer = new Equalizer(0, audioSessionId);
			equalizer.setEnabled(LaisimConfig.isEqEnable);
			bassBoost = new BassBoost(0, audioSessionId);
			bassBoost.setEnabled(LaisimConfig.isBassEnable);
			isSoundEffectSupported = true;
			return true;
		} catch (Exception ex) {
			isSoundEffectSupported = false;
			return false;
		} catch (UnsatisfiedLinkError ue) {
			isSoundEffectSupported = false;
			return false;
		}
	}

	public static void loadEffect(Context c, int id) {
		if (mAudioEffect == null)
			return;
		if (id == -1) {
			resetEffect();
			applyEffect();
			return;
		}

		// if the next effect is the same as the current effect. Do not load new
		// effect
		if (id == mAudioEffect.getId())
			return;

		DataBaseAccess db = DataBaseAccess.getInstance(c
				.getApplicationContext());
		mAudioEffect = db.getAudioEffect(id);
		applyEffect();
	}

	private static void resetEffect() {
		mAudioEffect.setId(-1);
		mAudioEffect.setBassboostEnable(false);
		mAudioEffect.setEqEnable(false);

		mAudioEffect.setBalance(50);
		mAudioEffect.setVolume(1000);
		mAudioEffect.setApplyMode(ApplyMode.CURRENT_SONG);
		mAudioEffect.setBassboostStrength(500);
		mAudioEffect.setEq14k(0);
		mAudioEffect.setEq3k6(0);
		mAudioEffect.setEq910(0);
		mAudioEffect.setEq230(0);
		mAudioEffect.setEq60(0);

	}

	private static void applyEffect() {
		if (equalizer == null)
			return;
		if (bassBoost == null)
			return;
		if (LaisimConfig.isEqEnable) {
			equalizer.setEnabled(mAudioEffect.isEqEnable());
		} else {
			equalizer.setEnabled(false);
		}
		if (LaisimConfig.isBassEnable)
			bassBoost.setEnabled(mAudioEffect.isBassboostEnable());

		if (equalizer == null)
			return;
		// eq
		try {
			short minValueOfBand = AudioEffectManager.equalizer
					.getBandLevelRange()[0];
			equalizer.setBandLevel((short) 0,
					(short) (mAudioEffect.getEq60() + minValueOfBand));
			equalizer.setBandLevel((short) 1,
					(short) (mAudioEffect.getEq230() + minValueOfBand));
			equalizer.setBandLevel((short) 2,
					(short) (mAudioEffect.getEq910() + minValueOfBand));
			equalizer.setBandLevel((short) 3,
					(short) (mAudioEffect.getEq3k6() + minValueOfBand));
			equalizer.setBandLevel((short) 4,
					(short) (mAudioEffect.getEq14k() + minValueOfBand));
			// bass boost
			if (bassBoost == null)
				return;
			bassBoost.setStrength((short) mAudioEffect.getBassboostStrength());
		} catch (RuntimeException re) {
			re.printStackTrace();
		}
	}

	public static AudioEffect getCurrentAudioEffect() {
		return mAudioEffect;
	}

	public static void release() {
		if (equalizer != null)
			equalizer.release();
		if (bassBoost != null)
			bassBoost.release();
		mAudioEffect = null;
	}
}
